import { wechatLogin } from '@/api/user.js'
import { refreshCurrentPage, getVM } from '@/utils/utils.js'
import store from '@/store'
import user from '../store/modules/user'

/**
 * 登录
 */
export function login() {
	uni.login({
		success: (res) => {
			wechatLogin({
				code: res.code
			}).then(userInfo => {
				if (userInfo.data.token) {
					store.commit('user/SET_TOKEN', userInfo.data.token)
					store.commit('user/SET_LOGIN', true);
					store.commit('user/SET_USERID', userInfo.data.userId);
					uni.showToast({
						icon: 'none',
						title: '登录成功'
					})
					refreshCurrentPage();
				} else { //未注册，跳转到授权
					uni.showToast({
						icon: 'none',
						title: '未注册，即将跳转'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/other/auth'
						})
					}, 700)
				}
			}).catch(err => {
				
			})
		},
		fail: (err) => {
			console.log(err);
		}
	})
}

/**
 * 退出登录
 */
export function logout(callback){
	store.commit('user/SET_LOGIN', false);
	store.commit('user/SET_TOKEN', "");
	store.commit('user/SET_USERID', "");
}